import 'package:chop/Model/order_img_entity.dart';
import 'package:chop/common/DioUtils.dart';
import 'package:chop/common/configs.dart';
import 'package:chop/widgets/LoginOutWidget.dart';
import 'package:chop/widgets/TextSizeWidget.dart';
import 'package:chop/widgets/ToastWidget.dart';
import 'package:chop/widgets/loadingDialog.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

Future<int> UploadImg(context,{var imgPath,OrderImg img})async{
  int code;
  showDialog<Null>(
    context: context, //BuildContext对象
    barrierDismissible: false,
    builder: (BuildContext context) {
      return LoadingDialog(
        //调用对话框
        text: 'Loading...',
      );
    },
  );
  await DioUtils.uploadImg(imgPath).then((a) {
    Map m = a;
    if(m['code'] == 0){
      img.address = m['fileName'];
      DioUtils.imgAdd(img).then((val){
        if(val.code == 0){
          Toast.toast(context,msg: "上传成功");
          code = 0;
//          Get.back();
        }
      });
    }
  });
  return code;
}


/*图片控件*/
Widget ImageView(context,int imgType,{imgs,imgPath,click1,click2}) {
  String str = "";
  Widget img;
  switch (imgType) {
    case 0:
      str += "货物运送拍照：  ";
      if(imgs.length == 0){
        if(imgPath != null){
          str += "已拍照";
          img = Container(
            child: GestureDetector(
              child: FixedSizeText("点击查看"),
              onTap: (){
                Get.defaultDialog(
                    title: "查看图片",
                    content:Container(
                      child: Image.file(imgPath),
                    ) );
              },
            ),
          );
//            img = Image.file(_imgPath);
        }else{
          str += "未拍照";
          img = Container(
            child: GestureDetector(
              child: FixedSizeText("点击拍照"),
              onTap: (){
                ShowBottom(context,
                    text: "拍照", backText: "本地图片", click: () {
                      click1();
                      Get.back();
                    }, callback: () {
                      click2();
                      Get.back();
                    });
              },
            ),
          );
        }
      }else{
        print(imgs.isEmpty);
        if(imgs.length != 0){
          var fl = true;
          imgs.forEach((val){
            print("____________${val.imgType}");
            if(val.imgType == "0"){
              str += "已拍照";
              img = Container(
                child: GestureDetector(
                  child: FixedSizeText("点击查看"),
                  onTap: (){
                    Get.defaultDialog(
                        title: "查看图片",
                        content:Container(
                          child: Image.network(Configs.URL+val.address),
                        ) );
                  },
                ),
              );
              fl = false;
            }
          });
          if(fl){
            str = "未拍照";
            img = Container(
              child: GestureDetector(
                child: FixedSizeText("点击拍照"),
                onTap: (){
                  ShowBottom(context,
                      text: "拍照", backText: "本地图片", click: () {
                        click1();
                        Get.back();
                      }, callback: () {
                        click2();
                        Get.back();
                      });
                },
              ),
            );
          }
        }
      }
      break;
    case 1:
      str += "送达拍照：  ";
      if(imgs.length == 1){
        if(imgPath != null){
          str += "已拍照";
          img = Container(
            child: GestureDetector(
              child: FixedSizeText("点击查看"),
              onTap: (){
                Get.defaultDialog(
                    title: "查看图片",
                    content:Container(
                        child: Image.file(imgPath)
                    ) );
              },
            ),
          );
//            img = Image.file(_imgPath);
        }else{
          str += "未拍照";
          img = Container(
            child: GestureDetector(
              child: FixedSizeText("点击拍照"),
              onTap: (){
                ShowBottom(context,
                    text: "拍照", backText: "本地图片", click: () {
                      click1();
                      Get.back();
                    }, callback: () {
                      click2();
                      Get.back();
                    });
              },
            ),
          );
        }
      }else{
        print(imgs.isEmpty);
        if(imgs.length != 1){
          var fl = true;
          imgs.forEach((val){
            print("____________${val.imgType}");
            if(val.imgType == "1"){
              str = "已拍照";
              img = Container(
                child: GestureDetector(
                  child: FixedSizeText("点击查看"),
                  onTap: (){
                    Get.defaultDialog(
                        title: "查看图片",
                        content:Container(
                          child: Image.network(Configs.URL+val.address),
                        ) );
                  },
                ),
              );
              fl = false;
            }
          });
          if(fl){
            str = "未拍照";
            img = Container(
              child: GestureDetector(
                child: FixedSizeText("点击拍照"),
                onTap: (){
                  ShowBottom(context,
                      text: "拍照", backText: "本地图片", click: () {
                        click1();
                        Get.back();
                      }, callback: () {
                        click2();
                        Get.back();
                      });
                },
              ),
            );
          }
        }
      }
      break;
    case 2:
      break;
    case 3:
      break;
  }
  return Row(
    children: <Widget>[
      Container(
        child: FixedSizeText(
          str,
          style: TextStyle(color: Colors.black45),
        ),
      ),
      Expanded(
          child: Container(
              alignment: Alignment.topRight,
              child:img
          ))
    ],
  );
//    if (imgPath == null) {
//      return Center(
//        child: Text("请拍摄运送物品照片"),
//      );
//    } else {
//      return Image.file(
//        imgPath,
//      );
//    }
}
